package bm.com.project.server.organization.controller;

import bm.com.framework.common.config.EnumRoute;
import bm.com.project.server.VerifyController;
import bm.com.project.server.common.dto.SearchBaseDTO;
import bm.com.project.server.organization.dto.BmOrgMemberDTO;
import bm.com.project.server.organization.dto.VoBmOrgMemberDTO;
import bm.com.project.server.organization.entity.BmOrgMemberEntity;
import bm.com.project.server.organization.service.MemberService;
import bm.com.framework.common.resp.PageVO;
import bm.com.framework.common.resp.Result;
import bm.com.framework.common.resp.ResultVO;
import bm.com.framework.common.unit.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//@Api("组织成员")
@RestController
@RequestMapping(EnumRoute.OrgRoute.Base)
public class OrgMemberController extends VerifyController {

    @Autowired
    MemberService memberService;

    @PostMapping(EnumRoute.OrgRoute.Member.INVITE)
    public ResultVO getInviteLink(
            @RequestParam(name = "limitNum",defaultValue = "5",required = false) int limitNum
    ) {
        if (limitNum <= 0) {
            return ResultVO.error("邀请数量不能为 0 ");
        }
        return memberService.getInviteLink(this.getOrgId(),this.getUserId(),limitNum);
    }

    @PostMapping(EnumRoute.OrgRoute.Member.JOIN_ORG)
    public ResultVO joinOrg(
            @RequestParam(name = "inviteCode") String inviteCode
    ){
        if (Tool.isStringEmpty(inviteCode)){
            return ResultVO.error("邀请码不能为空");
        }
        return memberService.joinOrg(this.getUserId(),inviteCode);
    }

//    @ApiOperation("审核加入组织的邀请")
    @PostMapping(EnumRoute.OrgRoute.Member.CHECK_INVITE)
    public ResultVO checkInvite(
            @RequestParam(name = "inviteRecordId") long inviteRecordId,
            @RequestParam(name = "isCheck",defaultValue = "1") int isCheck,
            @RequestParam(name = "departmentId") String departmentId
    ){
        return memberService.checkInvite(this.getUserId(),departmentId,isCheck);
    }

//    @ApiOperation("批量导入")
    @PostMapping(EnumRoute.OrgRoute.Member.BATCH_IMPORT)
    public ResultVO batchImport(@RequestBody Map<String,String> batchMobile){
        if(Tool.isStringEmpty(batchMobile.get("batchMobile"))){
            return ResultVO.error("手机号不能为空");
        }
        return memberService.batchImport(batchMobile.get("batchMobile"));
    }

//    @ApiOperation("成员列表")
    @PostMapping(EnumRoute.OrgRoute.Member.LIST)
    public ResultVO getList(@RequestBody SearchBaseDTO queue) {
        PageVO<VoBmOrgMemberDTO> list = memberService.getList(queue);
        return ResultVO.success(list);
    }

//    @ApiOperation("成员详情")
    @PostMapping(EnumRoute.OrgRoute.Member.DETAIL)
    public Result<BmOrgMemberEntity> getDetail(@RequestParam(name = "departmentId") String departmentId) {
        BmOrgMemberEntity detail = memberService.getDetail(departmentId);
        return new Result<BmOrgMemberEntity>().ok(detail);
    }

//    @ApiOperation("编辑成员")
    @PostMapping(EnumRoute.OrgRoute.Member.EDIT)
    public ResultVO edit(@RequestBody BmOrgMemberDTO bmOrgMember){
        return memberService.edit(bmOrgMember);
    }

//    @ApiOperation("设置管理员")
    @PostMapping(EnumRoute.OrgRoute.Member.SET_MANAGER)
    public ResultVO setManager(
            @RequestParam(name = "memberId") String memberId,
            @RequestParam(name = "isManager") int isManager
    ){
        return memberService.setManager(memberId,isManager);
    }
}
